package cn.gdk.gaodekecc.web.controller;

import cn.gdk.gaodekecc.service.IStocksService;
import cn.gdk.gaodekecc.domain.Stocks;
import cn.gdk.gaodekecc.query.StocksQuery;
import cn.gdk.gaodekecc.result.JSONResult;
import cn.gdk.gaodekecc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/stocks")
public class StocksController {

    @Autowired
    public IStocksService stocksService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Stocks stocks){
        if(stocks.getId()!=null){
            stocksService.updateById(stocks);
        }else{
            stocksService.insert(stocks);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        stocksService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(stocksService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(stocksService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody StocksQuery query){
        Page<Stocks> page = new Page<Stocks>(query.getPage(),query.getRows());
        page = stocksService.selectPage(page);
        return JSONResult.success(new PageList<Stocks>(page.getTotal(),page.getRecords()));
    }
    /**
     * 入库接口
     */

    @RequestMapping(value="/warehousing",method= RequestMethod.POST)
    public JSONResult warehousing(@RequestBody Stocks stocks){

        try {
            stocksService.warehousing(stocks);
            return JSONResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.error(e.getMessage());
        }


    }
    /**
     * 出库接口
     */
    @RequestMapping(value="/issue",method= RequestMethod.POST)
    public JSONResult issue(@RequestBody Stocks stocks){

        stocksService.issue(stocks);

        return JSONResult.success();
    }
    /**
     * 查询接口
     */

    @RequestMapping(value = "/{typeId}",method = RequestMethod.GET)
    public Stocks  selectBytypeId(@PathVariable("typeId") Integer typeId){
        Stocks stocks = stocksService.selectBytypeId(typeId);
        return stocks;
    }
}
